package com.example.a2205douyin.recommend;

import androidx.lifecycle.MutableLiveData;

import com.example.a2205douyin.baseMVVM.model.BaseRepo;
import com.example.a2205douyin.entity.BannerEntity;
import com.example.a2205douyin.entity.PinglunEntity;
import com.example.a2205douyin.entity.ResultEntity;
import com.example.a2205douyin.entity.VideoEntity;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class RecommendRepo extends BaseRepo {

    public void banner(MutableLiveData<BannerEntity> banSuccess,MutableLiveData<String> banFail){
        apiServer.banner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BannerEntity entity) {
                        banSuccess.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        banFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void video(int page, int size, MutableLiveData<VideoEntity> videoSuccess,MutableLiveData<String> videoFail){
        apiServer.video(page,size)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity entity) {
                        videoSuccess.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        videoFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void search(String keyword,MutableLiveData<VideoEntity> searchSucc,MutableLiveData<String> searchFail){
        apiServer.search(keyword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<VideoEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(VideoEntity entity) {
                        searchSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        searchFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //关注操作
    public void guanzhu(RequestBody body, MutableLiveData<ResultEntity> guanSucc,MutableLiveData<String> guanFail){
        apiServer.guanzhu(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity entity) {
                        guanSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        guanFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //点赞 --后台请求
    public void love(int videoId,MutableLiveData<ResultEntity> loveSucc,MutableLiveData<String> loveFail){
        apiServer.love(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity entity) {
                        loveSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        loveFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //取消点赞---后台请求
    public void nolove(int videoId,MutableLiveData<ResultEntity> noloveSucc,MutableLiveData<String> noloveFail){
        apiServer.nolove(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResultEntity entity) {
                        noloveSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        noloveFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    //获取评论
    public void getComment(int videoId, MutableLiveData<PinglunEntity> getSucc,MutableLiveData<String> getFail){
        apiServer.getComment(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PinglunEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PinglunEntity entity) {
                        getSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public void comment(RequestBody body,MutableLiveData<PinglunEntity> comSucc,MutableLiveData<String> comFail){
        apiServer.comment(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PinglunEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PinglunEntity entity) {
                        comSucc.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        comFail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
